package test.ground.netty.netty.my;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * 编码
 * @author wangshaopeng
 */
public class MessageEncoder extends MessageToByteEncoder<Message> {

    @Override
    protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf out) throws Exception {
        out.writeByte(msg.getType());
        out.writeLong(msg.getRequestId());
        byte[] data = msg.getBody().getBytes(Message.charset);
        out.writeInt(data.length);
        out.writeBytes(data);
    }
}
